package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.domain.Article;
import org.example.domain.Category;
import org.example.mapper.ArticleMapper;
import org.example.mapper.CategoryMapper;
import org.example.param.CategoryAddParam;
import org.example.param.CategoryFuzzyParam;
import org.example.param.CategoryUpdateParam;
import org.example.service.CategoryService;
import org.example.utils.HttpCode;
import org.example.utils.Result;
import org.example.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Result all() {
        List<Category> categoryList = categoryMapper.selectList(null);
        return Result.success(categoryList);
    }


    @Override
    public Result delete(Long id) {
        int total = categoryMapper.deleteById(id);
        if (total == 0){
            return Result.fail(HttpCode.DELETE_ERROR);
        }
        return Result.success();
    }

    /**
     * 添加时,判断是否有相同名称的类别
     * @param categoryAddParam
     * @return
     */
    @Override
    @Transactional
    public Result add(CategoryAddParam categoryAddParam) {
        // 去除左右空格
        categoryAddParam.setCategoryName(categoryAddParam.getCategoryName().trim());

        if (findCategoryByName(categoryAddParam.getCategoryName()).getData()!=null){
            return Result.fail(HttpCode.CATEGORY_EXIST);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddParam,category);
        category.setCreateDate(System.currentTimeMillis());
        int total = categoryMapper.insert(category);
        if (total == 0){
            return Result.fail(HttpCode.INSERT_ERROR);
        }
        return Result.success();
    }

    @Override
    public Result findCategoryByName(String categoryName) {
        if (!StringUtils.hasText(categoryName)){
            return Result.fail(HttpCode.PARAM_ERROR);
        }
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getCategoryName,categoryName);
        wrapper.last("limit 1");
        Category category = categoryMapper.selectOne(wrapper);
        return Result.success(category);
    }

    @Override
    public Result findCategoryById(Long id) {
        if (id == null) return Result.fail(HttpCode.PARAM_ERROR);
        Category category = categoryMapper.selectById(id);
        return Result.success(category);
    }

    @Override
    @Transactional
    public Result update(CategoryUpdateParam categoryUpdateParam) {
        Category data = (Category) findCategoryByName(categoryUpdateParam.getCategoryName().trim()).getData();
        if (data !=null && data.getId().longValue() != categoryUpdateParam.getId()){
            return Result.fail(HttpCode.CATEGORY_EXIST);
        }
        Category category = Category.builder()
                .id(categoryUpdateParam.getId())
                .categoryName(categoryUpdateParam.getCategoryName().trim())
                .avatar(categoryUpdateParam.getAvatar())
                .description(categoryUpdateParam.getDescription())
                .build();
        int total = categoryMapper.updateById(category);
        if (total == 0){
            return Result.fail(HttpCode.UPDATE_ERROR);
        }
        return Result.success();
    }

    @Override
    public Result fuzzy(CategoryFuzzyParam categoryFuzzyParam) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        // 条件
        wrapper.like(StringUtils.hasText(categoryFuzzyParam.getCategoryName()),Category::getCategoryName,categoryFuzzyParam.getCategoryName());
        wrapper.ge(categoryFuzzyParam.getStartDate()!=null,Category::getCreateDate,categoryFuzzyParam.getStartDate());
        wrapper.le(categoryFuzzyParam.getEndDate()!=null,Category::getCreateDate,categoryFuzzyParam.getEndDate());

        Page<Category> categoryPage = categoryMapper.selectPage(new Page<>(categoryFuzzyParam.getPageNum(), categoryFuzzyParam.getPageSize()),
                wrapper);
        return Result.success(categoryPage);
    }

    @Override
    public Result hot(Integer limit) {
        List<CategoryVo> categoryVoList = categoryMapper.hot(limit);
        return Result.success(categoryVoList);
    }

}
